package com.sjf.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sjf.common.Result;
import com.sjf.reggie.entity.Employee;
import com.sjf.reggie.mapper.EmployeeMapper;
import com.sjf.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;


/**
 * @author <huangtang>
 * @since 2022/4/25 11:48
 **/
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {


    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Result<Employee> login ( HttpServletRequest request , Employee employee ) {
        //将密码进行加密
        String Encode = DigestUtils.md5DigestAsHex (employee.getPassword ().getBytes(StandardCharsets.UTF_8));
        //根据用户名查询数据库
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<> ();
        queryWrapper.lambda ().eq ( Employee::getUsername , employee.getUsername () );
        Employee entity = employeeMapper.selectOne (queryWrapper);
        if(entity == null){
            //用户名不存在
           return Result.error ("用户名不存在");
        }
        //密码比对
        if(!Encode.equals (entity.getPassword ())){
            //密码错误
            return Result.error ("密码错误");
        }
        //查看员工状态
        if(entity.getStatus () == 0){
            //禁用
            return Result.error ("该员工已被禁用");
        }
        //登录成功
        request.getSession ().setAttribute ("employee" , entity.getId ());
        return Result.success (entity);
    }

    /**
     * 注销
     * @param request
     * @return
     */
    @Override
    public void logout ( HttpServletRequest request ) {
        //销毁session
        request.getSession ().removeAttribute ("employee");
    }

    /**
     * 新增员工
     * @param employee 员工信息
     * @return
     */
    @Override
    @Transactional
    public boolean add ( HttpServletRequest request,Employee employee ) {
        employee.setPassword (DigestUtils.md5DigestAsHex ("123456".getBytes ()));//默认密码
        //employee.setCreateTime (LocalDateTime.now ());
        //employee.setUpdateTime (LocalDateTime.now ());
        //获取当前用户
        Long id = (Long)request.getSession ().getAttribute ("employee");
        //employee.setCreateUser (id);
        //employee.setUpdateUser (id);
        return employeeMapper.insert (employee) > 0;
    }

    @Override
    public Page<Employee> selectList ( Integer page , Integer size , String name ) {
        Page<Employee> page1 = new Page (page , size);
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<> ();
        //判断name是否为空
        queryWrapper.lambda ().like (!ObjectUtils.isEmpty (name) , Employee::getName , name);
        return page(page1 , queryWrapper);

    }

    @Override
    public boolean edit (HttpServletRequest request, Employee employee ) {
        //UpdateWrapper<Employee> updateWrapper = new UpdateWrapper<> ();
        Long id = (Long)request.getSession ().getAttribute ("employee");

        employee.setName (employee.getName ());
        employee.setUsername (employee.getUsername ());
        employee.setPhone (employee.getPhone ());
        employee.setIdNumber (employee.getIdNumber ());
        employee.setStatus (employee.getStatus () );
        employee.setSex (employee.getSex ());
        //employee.setUpdateTime (LocalDateTime.now ());
        //employee.setUpdateUser (id);

        //updateWrapper.lambda ().eq (Employee::getId , employee.getId ())
        //        .set (!ObjectUtils.isEmpty (employee.getName ()) , Employee::getName , employee.getName ())
        //        .set (!ObjectUtils.isEmpty (employee.getPhone ()) , Employee::getPhone , employee.getPhone ())
        //        .set (!ObjectUtils.isEmpty (employee.getIdNumber ()) , Employee::getIdNumber , employee.getIdNumber ())
        //        .set (Employee::getUpdateUser , id)
        //        .set (Employee::getUpdateTime , LocalDateTime.now ())
        //        .set (!ObjectUtils.isEmpty (employee.getStatus ()) , Employee::getStatus , employee.getStatus ());
        return employeeMapper.updateById (employee) > 0;
    }
}
